package com.healthcloud.controller;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.SysConfigEntity;
import com.healthcloud.service.SysConfigService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import com.healthcloud.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 系统配置信息
 * Created by yu on 2017/6/6.
 */
@ApiIgnore
@Controller
@RequestMapping("/sys/config")
public class SysConfigController extends AbstractController{

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 所有配置列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:config:list")
    public String list(@RequestParam Map<String, Object> params){
        return "sys/config/configMgr";
    }


    /**
     * 所有配置列表
     */
    @RequestMapping("/search")
    @ResponseBody
    public Map<String, Object>  seartList(@ModelAttribute("pageParam")PageParam pageParam,SysConfigEntity entity){


        //查询列表数据
        List<SysConfigEntity> configList = sysConfigService.queryList(entity,pageParam);
        int total = sysConfigService.queryTotal(entity);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dataList", configList);
        map.put("iTotalRecords", total);
        map.put("iTotalDisplayRecords", total);
        map.put("sEcho", pageParam.getsEcho());

        return map;
    }

    @RequestMapping(value = "/getConfig")
    @ResponseBody
    public ApiRes getConfig(SysConfigEntity bean){
        //查询列表数据
        List<SysConfigEntity> configList = sysConfigService.queryList(bean, new PageParam());
        return  new ApiRes(true, configList,null,null);

    }


    /**
     * 所有配置列表
     */
    @RequestMapping("/tosave")
    @RequiresPermissions("sys:config:save")
    public String toSaveConfig (@RequestParam Map<String, Object> params){
        return "sys/config/addOrUpdateConfig";
    }


    /**
     * 保存配置
     */
    @SysLog("保存配置")
    @RequestMapping("/save")
    @ResponseBody
    public Object save(SysConfigEntity config){
        ValidatorUtils.validateEntity(config);

        sysConfigService.save(config);

        return ApiRes.SUCCESS;
    }


    /**
     * 修改配置
     */
    @RequestMapping("/toupdate")
    @RequiresPermissions("sys:config:update")
    public String update(@RequestParam("id") String id,Model model){

        SysConfigEntity config = sysConfigService.queryObjectByid(Long.valueOf(id));

        model.addAttribute("entity",config);

        return "sys/config/addOrUpdateConfig";
    }

    /**
     * 修改配置
     */
    @SysLog("修改配置")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(SysConfigEntity config){
        ValidatorUtils.validateEntity(config);

        sysConfigService.update(config);

        return ApiRes.SUCCESS;
    }

    /**
     * 删除配置
     */
    @SysLog("删除配置")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:config:delete")
    @ResponseBody
    public Object delete(Long[] ids){
        sysConfigService.deleteBatch(ids);

        return ApiRes.SUCCESS;
    }
}
